package org.example.cotroller.order;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.OrderUser;
import org.example.entity.PageBean;
import org.example.entity.Result;
import org.example.service.OrderUserService;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Resource
    private OrderUserService orderUserService;


    /**
     * 后台订单管理页面
     * */

    /**
     * 删除订单
     * */
    @GetMapping("/{orderId}")
    public Result deleteOrder(@PathVariable Integer orderId) {
        log.info("删除订单：{}",orderId);
        if (orderUserService.deleteOrderByOrderId(orderId))
            return Result.success();
        else return Result.error("失败");
    }

    /**
     * 获取全部订单
     * */
    @GetMapping("")
    public Result<PageBean<OrderUser>> getAllOrderByReason(
                                      @RequestParam(value = "orderIdBegin",required = false, defaultValue = "")String orderIdBegin,
                                      @RequestParam(value = "orderIdEnd",required = false, defaultValue = "")String orderIdEnd,
                                      @RequestParam(value = "userName",required = false, defaultValue = "")String username,
                                      @RequestParam(value = "status",required = false, defaultValue = "")String status,
                                      @RequestParam(value = "phone",required = false, defaultValue = "")String phone,
                                      @RequestParam(value = "beginTime",required = false, defaultValue = "")String beginTime,
                                      @RequestParam(value = "endTime",required = false, defaultValue = "")String endTime,
                                      @RequestParam(value = "pageSize",required = false, defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "page",required = false, defaultValue = "1")Integer page) {
        log.info("获取列表:");
        PageBean<OrderUser> goodsPageBean = orderUserService.listByReason(pageSize,page,
                orderIdBegin,orderIdEnd,status,phone,username,beginTime,endTime);
        return Result.success(goodsPageBean);

    }

    /**
     * 订单发货
     * */
    @GetMapping("/send")
    public Result orderChangeStatus(@RequestParam(value = "orderId")String orderId) {
        log.info("订单：{}发货",orderId);
        if (orderUserService.orderSend(orderId))
            return Result.success();
        else return Result.error("发货失败,请检查是否已发货");
    }
}
